#include "stdafx.h"
#include "EventScheme.h"
#include "MetaXmlHandler.h"
#include <comdef.h>

Trace::Trace(const char* pszClass, const char* pszMethod, const char* pszDirection)
{
  memset(m_szClass, 0, sizeof m_szClass);
  memset(m_szMethod, 0, sizeof m_szMethod);
  memset(m_szDirection, 0, sizeof m_szDirection);

  if (pszClass) strncpy(m_szClass, pszClass, sizeof m_szClass -1);
  if (pszMethod) strncpy(m_szMethod, pszMethod, sizeof m_szMethod -1);
  if (pszDirection) strncpy(m_szDirection, pszDirection, sizeof m_szDirection -1);
}

Trace::~Trace()
{

}

Trace::Trace(const Trace * pOther)
{
  memset(m_szClass, 0, sizeof m_szClass);
  memset(m_szMethod, 0, sizeof m_szMethod);
  memset(m_szDirection, 0, sizeof m_szDirection);

  if (pOther->m_szClass) strncpy(m_szClass, pOther->m_szClass, sizeof m_szClass -1);
  if (pOther->m_szMethod) strncpy(m_szMethod, pOther->m_szMethod, sizeof m_szMethod -1);
  if (pOther->m_szDirection) strncpy(m_szDirection, pOther->m_szDirection, sizeof m_szDirection -1);
}

const char* Trace::getClass() const 
{ 
  return m_szClass; 
}

const char* Trace::getMethod() const 
{ 
  return m_szMethod; 
}

const char* Trace::getDirection() const 
{ 
  return m_szDirection; 
}


const char* EventScheme::toXml() 
{
  std::list<std::string>::iterator m_itePatt;
  std::list<std::string>::iterator m_iteTrac;

  _bstr_t xml;
  xml = //"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"
        "<event severity=\""+_bstr_t(m_szEventSeverity)+"\">"
        "  <executable>"+_bstr_t(m_szExec)+"</executable>"
        "  <name>"+_bstr_t(m_szEventName)+"</name>"
        "  <category>"+_bstr_t(m_szEventCategory)+"</category>"
        "  <set>"
        "    <sequence name=\""+_bstr_t(m_szSeqName)+"\">";  

  int seq = 0;
  for (m_itePatt = m_liPattern.begin(); m_itePatt != m_liPattern.end(); ++m_itePatt) {
    ++seq;
    xml += "<pattern sequence=\""+_bstr_t(seq)+"\">"+_bstr_t((*m_itePatt).c_str())+"</pattern>";        
  }

  if (0 < m_liTrace.size()) {
    xml += "    <traces>";
    for (m_iteTrac = m_liTrace.begin(); m_iteTrac != m_liTrace.end(); ++m_iteTrac)
      xml += _bstr_t((*m_iteTrac).c_str());        
    xml += "    </traces>";
  }

  xml += "    </sequence>"
         "  </set>"
         "</event>";
  
  m_pszXml = new char[xml.length()+1];
  memset(m_pszXml, 0, xml.length()+1);
  strncpy(m_pszXml, xml, xml.length());
  return m_pszXml;
}

